﻿using System.Windows;
using System.Windows.Controls;
using Models;

namespace SuperQuery.UserControls.AddDatabaseWizard
{
    /// <summary>
    /// Interaction logic for ServerDetailsView.xaml
    /// </summary>
    public partial class ServerDetailsView : UserControl, WizardControl.Views.IPage, Views.IServerDetailsView
    {
        /// <summary>
        /// Constructor for the server details Wizard page
        /// </summary>
        public ServerDetailsView()
        {
            InitializeComponent();

            // Hook up the DataContext so that databinding will work
            DataContext = this;

            m_Presenter = new Presenters.ServerDetails(this);
        }

        #region Implementation of IPage

        /// <summary>
        /// An object containing data that this page can use
        /// </summary>
        public object InputData
        {
            get;
            set;
        }

        /// <summary>
        /// An object containing data that will be passed to the InputData property of the next page
        /// </summary>
        public object OutputData
        {
            get
            {
                // Return the server details
                return m_Presenter.GetServerObject();
            }
            set
            {
                // This is a read only property for this page so do nothing
                // This "set" section has to be here becuase of the IPage interface
            }
        }

        /// <summary>
        /// The number of this page
        /// </summary>
        /// <returns>A page number</returns>
        public int PageNumber()
        {
            return 1;
        }

        /// <summary>
        /// The caption to use for this page
        /// </summary>
        /// <returns>A page caption</returns>
        public string PageCaption()
        {
            return "Database server connection settings";
        }

        /// <summary>
        /// The state of the Finish button
        /// </summary>
        /// <returns>If true then Finishg will be active</returns>
        public bool AllowFinish()
        {
            return false;
        }

        /// <summary>
        /// The state of the Cancel button
        /// </summary>
        /// <returns>If true then the Cancel button will be active</returns>
        public bool AllowCancel()
        {
            return true;
        }

        /// <summary>
        /// The state of the Back button
        /// </summary>
        /// <returns>If true then the Back button will be active</returns>
        public bool AllowBack()
        {
            return false;
        }

        /// <summary>
        /// The state of the Forward button
        /// </summary>
        /// <returns>If true then the Forward button will be active</returns>
        public bool AllowForward()
        {
            return true;
        }

        /// <summary>
        /// Validate data on the current page
        /// </summary>
        /// <returns>If true then the next page will be displayed, otherwise we remain on the current page</returns>
        public bool ValidatePage()
        {
            return m_Presenter.ValidateServer();
        }

        /// <summary>
        /// The next page to progress to when forward is clicked
        /// </summary>
        /// <returns>The next page number</returns>
        public int NextPageNumber()
        {
            return WizardControl.Wizard.FINISH_PAGE;
        }

        /// <summary>
        /// Called just before the page is displayed
        /// </summary>
        public void PageActivated()
        {
            // Nothing to do here for this page
            return;
        }

        /// <summary>
        /// Called just after the page is validated, before the next page is displayed
        /// </summary>
        public void PageDeactivated()
        {
            // Nothing to do here for this page
            return;
        }

        #endregion

        #region Implementation of IServerDetailsView

        /// <summary>
        /// The name of the server we are getting databases for
        /// </summary>
        public string ServerName
        {
            get
            {
                return (string)GetValue(ServerNameProperty);
            }
            set
            {
                SetValue(ServerNameProperty, value);
            }
        }

        /// <summary>
        /// The user name to use when connecting to the server
        /// </summary>
        public string UserName
        {
            get
            {
                return (string)GetValue(UserNameProperty);
            }
            set
            {
                SetValue(UserNameProperty, value);
            }
        }

        /// <summary>
        /// The password to use when connecting to the server
        /// </summary>
        public string Password
        {
            get
            {
                return (string)GetValue(PasswordProperty);
            }
            set
            {
                SetValue(PasswordProperty, value);
            }
        }

        /// <summary>
        /// Whether or not to use Windows Authentication when connecting to the server
        /// </summary>
        public bool WindowsAuthentication
        {
            get
            {
                return (bool)GetValue(WindowsAuthenticationProperty);
            }
            set
            {
                SetValue(WindowsAuthenticationProperty, value);
            }
        }

        /// <summary>
        /// Display an error message to the user
        /// </summary>
        /// <param name="message">The error message to display</param>
        /// <param name="title">The title to use for the error message</param>
        public void DisplayError(string message, string title)
        {
            MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for ServerName.  This enables animation, styling, binding, etc... 
        /// </summary>
        public static readonly DependencyProperty ServerNameProperty =
            DependencyProperty.Register("ServerName", typeof(string), typeof(ServerDetailsView), new UIPropertyMetadata(null));

        /// <summary>
        /// Using a DependencyProperty as the backing store for UserName.  This enables animation, styling, binding, etc... 
        /// </summary>
        public static readonly DependencyProperty UserNameProperty =
            DependencyProperty.Register("UserName", typeof(string), typeof(ServerDetailsView), new UIPropertyMetadata(null));

        /// <summary>
        /// Using a DependencyProperty as the backing store for Password.  This enables animation, styling, binding, etc... 
        /// </summary>
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(ServerDetailsView), new UIPropertyMetadata(null));

        /// <summary>
        /// Using a DependencyProperty as the backing store for WindowsAuthentication.  This enables animation, styling, binding, etc... 
        /// </summary>
        public static readonly DependencyProperty WindowsAuthenticationProperty =
            DependencyProperty.Register("WindowsAuthentication", typeof(bool), typeof(ServerDetailsView), new UIPropertyMetadata(true));

        #endregion

        /// <summary>
        /// Presenter for this class
        /// </summary>
        private readonly Presenters.ServerDetails m_Presenter;
    }
}
